package com.metacube.questionbank.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.metacube.questionbank.dao.AnswerDAO;
import com.metacube.questionbank.model.Answer;
import com.metacube.questionbank.model.Question;
import com.metacube.questionbank.service.AnswerService;

/*
 * @author Team Neeraj Jain
 * */
public class AnswerServiceImpl implements AnswerService {
	@Autowired
	AnswerDAO answerDao;

	/*
	 * @param qId id of question This method returns the answers of the question
	 * whose id id is passed in the argument.
	 * 
	 * @return list of answers or null;
	 */
	public List<Answer> getAnswers(int qId) {
		List<Answer> answerList;
		try {
			answerList = answerDao.getAnswers(qId);
		} catch (Exception e) {
			answerList = null;
			System.out.println("Sql Exception in AnswerService");
		}
		return answerList;
	}

	/*
	 * @param userId id of user This method returns the answers which are given
	 * by a particular user.
	 * 
	 * @return list of answers or null
	 */
	public List<Answer> getAnswersByUserId(int userId) {
		List<Answer> answerList;
		try {
			answerList = answerDao.getAnswersByUserId(userId);
		} catch (Exception e) {
			answerList = null;
			System.out.println("Sql Exception in AnswerService");
		}
		return answerList;
	}

	/*
	 * @param userId id of user This method returns the answers which are liked
	 * by a particular user.
	 * 
	 * @return list of answers or null
	 */
	public List<Answer> getAnswerLikedByUser(int userId) {
		List<Answer> answerList;
		try {
			answerList = answerDao.getAnswerLikedByUser(userId);
		} catch (Exception e) {
			answerList = null;
			System.out.println("Sql Exception in AnswerService");
		}
		return answerList;
	}

	/*
	 * @param answer object of answer class This method adds an answer in the
	 * database.
	 */
	public void insertAnswer(Answer answer) {
		try {
			answerDao.insertAnswer(answer);

		} catch (Exception e) {
			System.out.println("Sql Exception in AnswerService");
		}
	}

	/*
	 * @param questions list of questions This method returns the list of answer
	 * count.That is,it finds answer count for each question in the list.
	 * 
	 * @return list of answer count
	 */
	public List<Integer> getAnswerCount(List<Question> questions) {
		int count;
		List<Integer> ansCount = new ArrayList<Integer>();
		try {
			for (int qCounter = 0; qCounter < questions.size(); qCounter++) {
				count = answerDao.getAnswerCount(questions.get(qCounter));
				ansCount.add(count);
			}
		} catch (Exception e) {
			ansCount = null;
			System.out.println("Sql Exception in AnswerService");
		}
		return ansCount;
	}

	/*
	 * @param questions list of questions This method returns the list of answer
	 * count.That is,it finds answer count for each question in the list.
	 * 
	 * @return list of answer count
	 */
	public Integer getAnswerCount(Question question) {
		int ansCount;
		try {
			ansCount = answerDao.getAnswerCount(question);
		} catch (Exception e) {
			ansCount = 0;
			System.out.println("Sql Exception in AnswerService");
		}
		return ansCount;
	}
	/*
	 * @param answerId id of answer
	 * This method returns the id of user who posted the answer
	 * @return userId id of user who posted the answer
	 * */
	public Integer getUserIdByAId(int answerId){
		int userId;
		userId=answerDao.getUserIdByAId(answerId);
		return userId;
	}
}
